//    Openbravo POS is a point of sales application designed for touch screens.
//    Copyright (C) 2007-2009 Openbravo, S.L.
//    http://www.openbravo.com/product/pos
//
//    This file is part of Openbravo POS.
//
//    Openbravo POS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Openbravo POS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Openbravo POS.  If not, see <http://www.gnu.org/licenses/>.

package cn.ekuma.epos.qkorder.dialog;

import cn.ekuma.epos.bean.util.erp.OrderLineHelper;
import cn.ekuma.epos.businesslogic.SerivceUtil;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.swing.dialog.AbstractKeyBoardJDialog;
import com.openbravo.data.basic.BasicException;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;


import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppView;
import com.openbravo.bean.erp.OrderLine;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.beans.JCalendarDialog;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.format.Formats;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.TitledBorder;

/**
 *
 * @author adrianromero
 */
public class JOrderLineEditDialog  extends AbstractKeyBoardJDialog{
    
    private OrderLine returnLine;
    private OrderLine m_oLine;
    private OrderType ordeType;
    private boolean m_bunitsok;
    private boolean m_bpriceok;
    private int active;
    private final static int activeCount=3;
    private I_DataLogicERP dlSales;
	private JButton btnValidityDate;
	private JTextArea lotNo;

    /** Creates new form JOrderLineEditDialog 
     * @wbp.parser.constructor*/
    private JOrderLineEditDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    /** Creates new form JOrderLineEditDialog */
    private JOrderLineEditDialog(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    private OrderLine init(AppView app, OrderLine oLine,OrderType ordeType,I_DataLogicERP dlSales) throws BasicException {
        // Inicializo los componentes
        if (oLine.getTaxInfo() == null) {
            throw new BasicException(AppLocal.getIntString("message.cannotcalculatetaxes"));
        }

        this.ordeType=ordeType;

        this.dlSales =dlSales;
        m_oLine = oLine.copyOrderLine();
        m_bunitsok = true;
        m_bpriceok = true;

        m_jName.setEnabled(m_oLine.getProductID() == null);
        m_jName.setText(m_oLine.getProperty("product.name"));
        m_jUnits.setDoubleValue(OrderLineHelper.getMultiply(oLine));
        m_jPrice.setDoubleValue(oLine.getPrice());
        m_jPriceTax.setDoubleValue(OrderLineHelper.getPriceTax(oLine));
        m_jTaxrate.setText(oLine.getTaxInfo().getName());
        jPriceRant.setDoubleValue(oLine.getPriceRate()*100); 
        
        m_jName.addPropertyChangeListener("Edition", new RecalculateName());
        m_jUnits.addPropertyChangeListener("Edition", new RecalculateUnits());
        m_jPrice.addPropertyChangeListener("Edition", new RecalculatePrice());
        m_jPriceTax.addPropertyChangeListener("Edition", new RecalculatePriceTax());
        jPriceRant.addPropertyChangeListener("Edition", new RecalculatePriceRant());

        m_jName.addEditorKeys(m_jKeys);
        m_jUnits.addEditorKeys(m_jKeys);
        m_jPrice.addEditorKeys(m_jKeys);
        m_jPriceTax.addEditorKeys(m_jKeys);
        jPriceRant.addEditorKeys(m_jKeys);
        
        active=1;
        if(oLine.isServiceOrderLine()){
            jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(oLine.getStartData()));
            jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(oLine.getEndData()));
            btnDateStart.setEnabled(true);
            btnDateEnd.setEnabled(true);
            m_jUnits.setEnabled(false);
            m_jPrice.activate();
            lotNo.setEnabled(false);
            btnValidityDate.setEnabled(false);
            lotNo.setText(null);
            validityDate.setText(null);
        }else{
            btnDateStart.setEnabled(false);
            btnDateEnd.setEnabled(false);
            m_jUnits.activate();
            if(oLine.getProductID()!=null){
                 lotNo.setText(oLine.getLotNo());
        	     validityDate.setText(Formats.TIMESTAMP.formatValue(oLine.getValidityDate()));
                 lotNo.setEnabled(true);
                 btnValidityDate.setEnabled(true);
            }else{
            	 lotNo.setEnabled(false);
                 btnValidityDate.setEnabled(false);
                 lotNo.setText(null);
                 validityDate.setText(null);
            }
        }
        printTotals();
        getRootPane().setDefaultButton(m_jButtonOK);   
        returnLine = null;
        setVisible(true);
      
        return returnLine;
    }
    
    private void printTotals() { 
        if (m_bunitsok && m_bpriceok) {
            m_jSubtotal.setText(OrderLineHelper.printSubValue(m_oLine));
            m_jTotal.setText(OrderLineHelper.printValue(m_oLine));
            m_jButtonOK.setEnabled(true);
       } else {
            m_jSubtotal.setText(null);
            m_jTotal.setText(null);
            m_jButtonOK.setEnabled(false);
        }
    }
     @Override
    protected void enterKeyProc() {
        if(m_jButtonOK.isEnabled())
            m_jButtonOKActionPerformed(null);
    }

    @Override
    protected void escapeKeyProc() {
         dispose();
    }

    @Override
    protected void upKeyProc() {
      if(!m_jPrice.isEnabled())
            return;
        switch(active){
            case 1:
               m_jUnits.deactivate();
               m_jPrice.deactivate();
               jPriceRant.activate();
               active=activeCount;
               break;
            case 2:
               m_jUnits.activate();
               m_jPrice.deactivate();
               jPriceRant.deactivate();
               active--;
               break;
            case 3:
               m_jUnits.deactivate();
               m_jPrice.activate();
               jPriceRant.deactivate();
               active--;
               break;
       }
    }

    @Override
    protected void downKeyProc() {
      if(!m_jPrice.isEnabled())
            return;
      switch(active){
            case 1:
               m_jUnits.deactivate();
               m_jPrice.activate();
               jPriceRant.deactivate();
               active++;
               break;
            case 2:
               m_jUnits.deactivate();
               m_jPrice.deactivate();
               jPriceRant.activate();
               active++;
               break;
            case 3:
               m_jUnits.activate();
               m_jPrice.deactivate();
               jPriceRant.deactivate();
               active=1;
               break;
       }
    }

    
    private class RecalculateUnits implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent evt) {
            Double value = m_jUnits.getDoubleValue();
            if (value == null || value == 0.0) {
                m_bunitsok = false;
            } else {
                m_oLine.setMultiply(value);
                m_bunitsok = true;                
            }
            printTotals();
        }
    }
    
    private class RecalculatePrice implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent evt) {
            Double value = m_jPrice.getDoubleValue();
            if (value == null || value == 0.0) {
                m_bpriceok = false;
            } else {
                m_oLine.setPrice(value);
                m_jPriceTax.setDoubleValue(OrderLineHelper.getPriceTax(m_oLine));
                m_bpriceok = true;
            }
            printTotals();
        }
    }    
    
    private class RecalculatePriceTax implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent evt) {
            Double value = m_jPriceTax.getDoubleValue();
            if (value == null || value == 0.0) {
                // m_jPriceTax.setValue(m_oLine.getPriceTax());
                m_bpriceok = false;
            } else {
                m_oLine.setPriceTax(value);
                m_jPrice.setDoubleValue(m_oLine.getPrice());
                m_bpriceok = true;
            }
            printTotals();
        }
    }   
    
    private class RecalculateName implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent evt) {
            m_oLine.setProperty("product.name", m_jName.getText()==null?"":m_jName.getText());
        }
    }   
        
    
    public static OrderLine showMessage(Component parent, AppView app, OrderLine oLine,OrderType ordertype,I_DataLogicERP dlSales) throws BasicException {
        Window window = getWindow(parent);     
        JOrderLineEditDialog myMsg;
        if (window instanceof Frame) { 
            myMsg = new JOrderLineEditDialog((Frame) window, true);
        } else {
            myMsg = new JOrderLineEditDialog((Dialog) window, true);
        }
        return myMsg.init(app, oLine,ordertype, dlSales);
    }        

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        m_jName = new com.openbravo.editor.JEditorString();
        m_jUnits = new com.openbravo.editor.JEditorDouble();
        m_jPrice = new com.openbravo.editor.JEditorCurrency();
        m_jPriceTax = new com.openbravo.editor.JEditorCurrency();
        m_jTaxrate = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        m_jTotal = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        m_jSubtotal = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPriceRant = new com.openbravo.editor.JEditorIntegerPositive();
        jLabel9 = new javax.swing.JLabel();
        jTxtStartDate = new javax.swing.JTextField();
        btnDateStart = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jTxtEndDate = new javax.swing.JTextField();
        btnDateEnd = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        m_jButtonOK = new javax.swing.JButton();
        m_jButtonCancel = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        m_jKeys = new com.openbravo.editor.JEditorKeys();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(AppLocal.getIntString("label.editline")); // NOI18N

        jPanel5.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(null);

        jLabel1.setText(AppLocal.getIntString("label.price")); // NOI18N
        jPanel2.add(jLabel1);
        jLabel1.setBounds(10, 80, 90, 15);

        jLabel2.setText(AppLocal.getIntString("label.units")); // NOI18N
        jPanel2.add(jLabel2);
        jLabel2.setBounds(10, 50, 90, 15);

        jLabel3.setText(AppLocal.getIntString("label.pricetax")); // NOI18N
        jPanel2.add(jLabel3);
        jLabel3.setBounds(10, 110, 90, 15);

        jLabel4.setText(AppLocal.getIntString("label.item")); // NOI18N
        jPanel2.add(jLabel4);
        jLabel4.setBounds(10, 20, 90, 15);
        jPanel2.add(m_jName);
        m_jName.setBounds(100, 20, 270, 25);
        jPanel2.add(m_jUnits);
        m_jUnits.setBounds(100, 50, 240, 25);
        jPanel2.add(m_jPrice);
        m_jPrice.setBounds(100, 80, 240, 25);
        jPanel2.add(m_jPriceTax);
        m_jPriceTax.setBounds(100, 110, 240, 25);

        m_jTaxrate.setBackground(javax.swing.UIManager.getDefaults().getColor("TextField.disabledBackground"));
        m_jTaxrate.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        m_jTaxrate.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("Button.darkShadow")), javax.swing.BorderFactory.createEmptyBorder(1, 4, 1, 4)));
        m_jTaxrate.setOpaque(true);
        m_jTaxrate.setPreferredSize(new java.awt.Dimension(150, 25));
        m_jTaxrate.setRequestFocusEnabled(false);
        jPanel2.add(m_jTaxrate);
        m_jTaxrate.setBounds(100, 170, 210, 25);

        jLabel5.setText(AppLocal.getIntString("label.tax")); // NOI18N
        jPanel2.add(jLabel5);
        jLabel5.setBounds(10, 170, 90, 15);

        jLabel6.setText(AppLocal.getIntString("label.totalcash")); // NOI18N
        jPanel2.add(jLabel6);
        jLabel6.setBounds(10, 230, 90, 15);

        m_jTotal.setBackground(javax.swing.UIManager.getDefaults().getColor("TextField.disabledBackground"));
        m_jTotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        m_jTotal.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("Button.darkShadow")), javax.swing.BorderFactory.createEmptyBorder(1, 4, 1, 4)));
        m_jTotal.setOpaque(true);
        m_jTotal.setPreferredSize(new java.awt.Dimension(150, 25));
        m_jTotal.setRequestFocusEnabled(false);
        jPanel2.add(m_jTotal);
        m_jTotal.setBounds(100, 230, 210, 25);

        jLabel7.setText(AppLocal.getIntString("label.subtotalcash")); // NOI18N
        jPanel2.add(jLabel7);
        jLabel7.setBounds(10, 200, 90, 15);

        m_jSubtotal.setBackground(javax.swing.UIManager.getDefaults().getColor("TextField.disabledBackground"));
        m_jSubtotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        m_jSubtotal.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("Button.darkShadow")), javax.swing.BorderFactory.createEmptyBorder(1, 4, 1, 4)));
        m_jSubtotal.setOpaque(true);
        m_jSubtotal.setPreferredSize(new java.awt.Dimension(150, 25));
        m_jSubtotal.setRequestFocusEnabled(false);
        jPanel2.add(m_jSubtotal);
        m_jSubtotal.setBounds(100, 200, 210, 25);

        jLabel8.setText(AppLocal.getIntString("label.priceRate")); // NOI18N
        jPanel2.add(jLabel8);
        jLabel8.setBounds(10, 140, 90, 15);
        jPanel2.add(jPriceRant);
        jPriceRant.setBounds(100, 140, 240, 25);

        jLabel9.setText(AppLocal.getIntString("Label.StartDate")); // NOI18N
        jPanel2.add(jLabel9);
        jLabel9.setBounds(10, 260, 80, 15);

        jTxtStartDate.setEditable(false);
        jTxtStartDate.setMinimumSize(new java.awt.Dimension(6, 15));
        jTxtStartDate.setPreferredSize(new java.awt.Dimension(150, 22));
        jPanel2.add(jTxtStartDate);
        jTxtStartDate.setBounds(100, 260, 150, 22);

        btnDateStart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
        btnDateStart.setFocusable(false);
        btnDateStart.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDateStart.setPreferredSize(new java.awt.Dimension(55, 25));
        btnDateStart.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDateStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDateStartActionPerformed(evt);
            }
        });
        jPanel2.add(btnDateStart);
        btnDateStart.setBounds(260, 260, 55, 25);

        jLabel10.setText(AppLocal.getIntString("Label.EndDate")); // NOI18N
        jPanel2.add(jLabel10);
        jLabel10.setBounds(10, 290, 90, 15);

        jTxtEndDate.setEditable(false);
        jTxtEndDate.setPreferredSize(new java.awt.Dimension(150, 22));
        jPanel2.add(jTxtEndDate);
        jTxtEndDate.setBounds(100, 290, 150, 22);

        btnDateEnd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
        btnDateEnd.setFocusable(false);
        btnDateEnd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDateEnd.setPreferredSize(new java.awt.Dimension(55, 25));
        btnDateEnd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDateEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDateEndActionPerformed(evt);
            }
        });
        jPanel2.add(btnDateEnd);
        btnDateEnd.setBounds(260, 290, 55, 25);

        jPanel5.add(jPanel2, java.awt.BorderLayout.CENTER);
        
        JLabel label = new JLabel();
        label.setText(AppLocal.getIntString("label.validityDate"));
        label.setBounds(10, 327, 90, 15);
        jPanel2.add(label);
        
        validityDate = new JTextField();
        validityDate.setPreferredSize(new Dimension(150, 22));
        validityDate.setMinimumSize(new Dimension(6, 15));
        validityDate.setEditable(false);
        validityDate.setBounds(100, 323, 150, 22);
        jPanel2.add(validityDate);
        
        btnValidityDate = new JButton(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png")));
        btnValidityDate.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		btnDateValidityDateActionPerformed(e);
        	}
        });
        btnValidityDate.setVerticalTextPosition(SwingConstants.BOTTOM);
        btnValidityDate.setPreferredSize(new Dimension(55, 25));
        btnValidityDate.setHorizontalTextPosition(SwingConstants.CENTER);
        btnValidityDate.setFocusable(false);
        btnValidityDate.setBounds(260, 322, 55, 25);
        jPanel2.add(btnValidityDate);

        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        m_jButtonOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_ok.png"))); // NOI18N
        m_jButtonOK.setText(AppLocal.getIntString("Button.OK")); // NOI18N
        m_jButtonOK.setFocusPainted(false);
        m_jButtonOK.setFocusable(false);
        m_jButtonOK.setMargin(new java.awt.Insets(8, 16, 8, 16));
        m_jButtonOK.setRequestFocusEnabled(false);
        m_jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_jButtonOKActionPerformed(evt);
            }
        });
        jPanel1.add(m_jButtonOK);

        m_jButtonCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png"))); // NOI18N
        m_jButtonCancel.setText(AppLocal.getIntString("Button.Cancel")); // NOI18N
        m_jButtonCancel.setFocusPainted(false);
        m_jButtonCancel.setFocusable(false);
        m_jButtonCancel.setMargin(new java.awt.Insets(8, 16, 8, 16));
        m_jButtonCancel.setRequestFocusEnabled(false);
        m_jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_jButtonCancelActionPerformed(evt);
            }
        });
        jPanel1.add(m_jButtonCancel);

        jPanel5.add(jPanel1, java.awt.BorderLayout.SOUTH);

        getContentPane().add(jPanel5, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel4.setLayout(new javax.swing.BoxLayout(jPanel4, javax.swing.BoxLayout.Y_AXIS));
        jPanel4.add(m_jKeys);

        jPanel3.add(jPanel4, java.awt.BorderLayout.NORTH);

        getContentPane().add(jPanel3, java.awt.BorderLayout.EAST);
        
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportBorder(new TitledBorder(null, AppLocal.getIntString("label.lotNo"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        jPanel3.add(scrollPane, BorderLayout.CENTER);
        
        lotNo = new JTextArea();
        scrollPane.setViewportView(lotNo);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-580)/2, (screenSize.height-421)/2, 580, 446);
    }// </editor-fold>//GEN-END:initComponents

    private void m_jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jButtonCancelActionPerformed
        dispose();
    }//GEN-LAST:event_m_jButtonCancelActionPerformed

    private void m_jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jButtonOKActionPerformed
        try {
            if (m_oLine.isServiceOrderLine() && ordeType.isServiceCheck() && !SerivceUtil.isServiceAble(dlSales, m_oLine.getServiceId(), m_oLine.getStartData(), m_oLine.getEndData())) {
                 new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.serviceNoAbleInThisTime")).show(this);
                 return ;
            }
        } catch (BasicException ex) {
           new MessageInf(MessageInf.SGN_WARNING, ex.getMessage()).show(this);
           return;
        }
        m_oLine.setLotNo(lotNo.getText());
        returnLine = m_oLine;
        dispose();

    }//GEN-LAST:event_m_jButtonOKActionPerformed

    private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateStartActionPerformed
        Date date;
        try {
            date = (Date) Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
        } catch (BasicException e) {
            date = null;
        }
        date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
            jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(date));
            m_oLine.setStartData(date);
            m_jUnits.setDoubleValue(OrderLineHelper.getMultiply(m_oLine));
            printTotals();
        }
}//GEN-LAST:event_btnDateStartActionPerformed

    private void btnDateEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateEndActionPerformed
        Date date;
        try {
            date = (Date) Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());
        } catch (BasicException e) {
            date = null;
        }
        date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
            jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(date));
            m_oLine.setEndData(date);
            m_jUnits.setDoubleValue(OrderLineHelper.getMultiply(m_oLine));
            printTotals();
        }
}//GEN-LAST:event_btnDateEndActionPerformed
    
    private void btnDateValidityDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateStartActionPerformed
        Date date;
        try {
            date = (Date) Formats.TIMESTAMP.parseValue(validityDate.getText());
        } catch (BasicException e) {
            date = null;
        }
        date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
        	validityDate.setText(Formats.TIMESTAMP.formatValue(date));
            m_oLine.setValidityDate(date);
        }
}//GEN-LAST:event_btnDateStartActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDateEnd;
    private javax.swing.JButton btnDateStart;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private com.openbravo.editor.JEditorIntegerPositive jPriceRant;
    private javax.swing.JTextField jTxtEndDate;
    private javax.swing.JTextField jTxtStartDate;
    private javax.swing.JButton m_jButtonCancel;
    private javax.swing.JButton m_jButtonOK;
    private com.openbravo.editor.JEditorKeys m_jKeys;
    private com.openbravo.editor.JEditorString m_jName;
    private com.openbravo.editor.JEditorCurrency m_jPrice;
    private com.openbravo.editor.JEditorCurrency m_jPriceTax;
    private javax.swing.JLabel m_jSubtotal;
    private javax.swing.JLabel m_jTaxrate;
    private javax.swing.JLabel m_jTotal;
    private com.openbravo.editor.JEditorDouble m_jUnits;
    private JTextField validityDate;
    // End of variables declaration//GEN-END:variables


     private class RecalculatePriceRant implements PropertyChangeListener {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            Double value = jPriceRant.getDoubleValue();
            if (value == null||value<0||value>100) {
                m_bunitsok = false;
            } else {
                m_oLine.setPriceRate(value/100);
                m_bunitsok = true;
            }
            printTotals();
        }
    }
}
